﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.Outstorage
{
    public partial class ListOutStorageDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["out_proid"] != null && Session["check_user_id"] != null && Session["login"] != null)
                {
                    DataSet ds_user = Session["login"] as DataSet;

                    string user_id = ds_user.Tables[0].Rows[0]["user_id"].ToString();

                    string proid = Session["out_proid"].ToString();
                    int check_user_id = Convert.ToInt32(Session["check_user_id"].ToString());

                    BindGV(proid, check_user_id,false,user_id);

                }
                else
                {
                    Maticsoft.Common.MessageBox.Show(this,"由于长时间未操作，登录信息已过期，请重新登陆!");
                }
               
            }
        }
        //check_user_id用处不明确，从ListOutStorage.aspx页面跳转过来的时候加上的，在此处用处不明确
        private void BindGV(string proid, int check_user_id,bool isSel,string user_id)
        {
            dzs.BLL.out_storageList om = new BLL.out_storageList();

            string where = " 1=1 and cguid=" + user_id;//登陆人仓管员所看到的自己仓库的物料

            if (!isSel)//如果不是查询
            {
                where += " and proid='" + proid + "' and check_user_id=" + check_user_id;
            }

            if (!string.IsNullOrEmpty(this.txt_batchID.Text))
            {
                where += " and out_storagelist_batchid like '%"+this.txt_batchID.Text+"%' ";
            }

            DataSet ds = om.GetCheckUserDetail(where);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();


            DataSet ds_batchId = om.GetBatchId("");

            this.ddl_batchid.DataSource = ds_batchId;
            this.ddl_batchid.DataTextField = "out_storagelist_batchid";
            this.ddl_batchid.DataValueField = "out_storagelist_batchid";
            this.ddl_batchid.DataBind();
      
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            if (Session["out_proid"] != null && Session["check_user_id"] != null && Session["login"] != null)
            {
                DataSet ds_user = Session["login"] as DataSet;

                string user_id = ds_user.Tables[0].Rows[0]["user_id"].ToString();

                string proid = Session["out_proid"].ToString();
                int check_user_id = Convert.ToInt32(Session["check_user_id"].ToString());

                BindGV(proid, check_user_id, false, user_id);
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName =="lbn_outStorage")
            {
                if (e.CommandArgument != null)
                {

                    Session.Remove("out_storageList_id");//去除以前的记录

                    string out_storageList_id = e.CommandArgument.ToString();

                    Session["out_storageList_id"] = out_storageList_id;
                    //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>f_open('"+proid+"')</script>");
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPop('FramePopOutStorage.aspx','900px','390px');</script>");

                    //Response.Redirect("ListOutStorageByVen.aspx");
                }
                
            }
            if (e.CommandName == "lbn_print")
            {
                if (e.CommandArgument != null)
                {
                    #region 以前的方法，从out_storage中读出库时间
                    //Session.Remove("print_out_storageList_id");//去除以前的记录

                    //string out_storageList_id = e.CommandArgument.ToString();

                    //Session["print_out_storageList_id"] = out_storageList_id;

                    //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPopWin('PrintOutStorage.aspx','600px','800px');</script>");
                    #endregion

                    string batchID = e.CommandArgument.ToString();

                    if (!string.IsNullOrEmpty(batchID))
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPopWin('PrintOutStorage.aspx?batchid=" + batchID + "','600px','800px');</script>");
                    }
                }
            }
        }

        protected void btn_reload_Click(object sender, EventArgs e)
        {

            if (Session["out_proid"] != null && Session["check_user_id"] != null && Session["login"] != null)
            {
                DataSet ds_user = Session["login"] as DataSet;

                string user_id = ds_user.Tables[0].Rows[0]["user_id"].ToString();

                string proid = Session["out_proid"].ToString();
                int check_user_id = Convert.ToInt32(Session["check_user_id"].ToString());

                BindGV(proid, check_user_id, false, user_id);
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType ==  DataControlRowType.DataRow)
            {
                Label lbl_out_storageList_id = e.Row.FindControl("lbl_out_storageList_id") as Label;
                Label lbl_material_id = e.Row.FindControl("lbl_material_id") as Label;
                Label lbl_status = e.Row.FindControl("lbl_status") as Label;
                Label lbl_out_storage_count = e.Row.FindControl("lbl_out_storage_count") as Label;
                LinkButton lbn_print = e.Row.FindControl("lbn_print") as LinkButton;
                
                
                dzs.BLL.out_storage om = new dzs.BLL.out_storage();

                if (!string.IsNullOrEmpty(lbl_out_storageList_id.Text) && !string.IsNullOrEmpty(lbl_material_id.Text)
                    && !string.IsNullOrEmpty(lbl_out_storage_count.Text))
                {
                    int olid = Convert.ToInt32(lbl_out_storageList_id.Text);
                    int mid = Convert.ToInt32(lbl_material_id.Text);
                    decimal alreadyCount = 0;
                    decimal out_storage_count = Convert.ToDecimal(lbl_out_storage_count.Text);

                    DataSet ds = om.GetList(" out_storageList_id=" + olid + " and material_id="+mid);

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        alreadyCount += Convert.ToDecimal(row["out_storage_count"]);
                    }

                    if (alreadyCount == out_storage_count)
                    {
                        lbl_status.Text = "已完成";
                        lbn_print.Enabled = true;
                    }
                    else
                    {
                        lbl_status.Text = "未完成";
                        lbn_print.Enabled = true;
                    }
                    
                }
            }
        }
      
        //按出库单号打印
        protected void btn_printByPer_Click(object sender, EventArgs e)
        {
            string batchID = this.ddl_batchid.SelectedValue;

            if (!string.IsNullOrEmpty(batchID))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPopWin('PrintOutStorage.aspx?batchid="+batchID+"','600px','800px');</script>");
            }

            //string receive_userName = this.ddl_receivePerson.SelectedValue;
            //string receive_address = this.ddl_receiveAdd.SelectedValue;
            //string out_time = this.ddl_outTime.SelectedValue;

            //if (string.IsNullOrEmpty(receive_userName))
            //{
            //    Maticsoft.Common.MessageBox.Show(this, "请选择接收人!");

            //    return;
            //}
            //if (string.IsNullOrEmpty(receive_address) || receive_address == "-1")
            //{
            //    Maticsoft.Common.MessageBox.Show(this,"请选择收货地点!");
               
            //    return;
            //}



            //List<string> li_oid = new List<string>();

            //for (int i = 0; i < this.GridView1.Rows.Count; i++)
            //{
            //    Label lbl_status = GridView1.Rows[i].FindControl("lbl_status") as Label;
            //    Label lbl_out_storageList_id = GridView1.Rows[i].FindControl("lbl_out_storageList_id") as Label;//出库单ID
            //    //Label lbl_material_id = GridView1.Rows[i].FindControl("lbl_material_id") as Label;//物料ID
            //    //Label lbl_out_storage_count = GridView1.Rows[i].FindControl("lbl_out_storage_count") as Label;//预出库数量

            //    if (lbl_status.Text == "已完成")
            //    {
            //        string oid = lbl_out_storageList_id.Text + "|" + receive_userName + "|" + receive_address + "|" + out_time;
                    
            //        li_oid.Add(oid);
            //    }

            //}

            //Session.Remove("li_oid_add");

            //Session["li_oid_add"] = li_oid;

            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPopWin('PrintOutStorage.aspx?type=r_add','600px','800px');</script>");
        }
       

        //查询
        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["out_proid"] != null && Session["check_user_id"] != null && Session["login"] != null)
            {
                DataSet ds_user = Session["login"] as DataSet;

                string user_id = ds_user.Tables[0].Rows[0]["user_id"].ToString();

                string proid = Session["out_proid"].ToString();
                int check_user_id = Convert.ToInt32(Session["check_user_id"].ToString());

                BindGV(proid, check_user_id, false, user_id);

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "由于长时间未操作，登录信息已过期，请重新登陆!");
            }

        }
       
    }
}